using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LibraryAutomation.VisualAuditorySources
{
    

    public partial class frmVASAdd : Form
    {
        string stSQL = "";
        DataSet ds = null;
        int rowsAffected = 0;

        public frmVASAdd()
        {
            InitializeComponent();
        }

        private void frmVASAdd_Load(object sender, EventArgs e)
        {
            FillComboBox(ref cmbType);
            FillListView(ref lvwSources);
        }

        private void FillListView(ref ListView lvwSources)
        {
            stSQL = "SELECT var_id,var_name,var_description FROM visual_auditory_resources LEFT JOIN var_types ON visual_auditory_resources.var_type = var_types.var_type ORDER BY var_name ASC";
            ds = new DataSet();

            if (datalayer.GetDataFromDB(datalayer.default_NetLibDB_Projeler_ConnectionString, stSQL, ref ds))
            {
                if (ds.Tables.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        lvwSources.Items.Add(ds.Tables[0].Rows[i].ItemArray[1].ToString().Trim());
                        lvwSources.Items[lvwSources.Items.Count - 1].SubItems.Add(ds.Tables[0].Rows[i].ItemArray[2].ToString().Trim());
                        lvwSources.Items[lvwSources.Items.Count - 1].Tag = ds.Tables[0].Rows[i].ItemArray[0].ToString().Trim();
                    }
                }
            }

        }

        private void FillComboBox(ref ComboBox cmbType)
        {
            stSQL = "SELECT * FROM var_types ORDER BY var_description ASC";
            ds = new DataSet();
            VarTypes vt = new VarTypes();

            if (datalayer.GetDataFromDB(datalayer.default_NetLibDB_Projeler_ConnectionString, stSQL, ref ds))
            {
                if (ds.Tables.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count ; i++)
                    {
                        vt.ID = ds.Tables[0].Rows[i].ItemArray[0].ToString();
                        vt.Description = ds.Tables[0].Rows[i].ItemArray[1].ToString();
                        cmbType.Items.Add(vt);
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddType_Click(object sender, EventArgs e)
        {
            if (txtTypeName.Text.Trim() != "")
            {
                stSQL = "INSERT INTO var_types (var_description) VALUES ( '"+ txtTypeName.Text.Trim() + "' )";

                if (datalayer.Execute(datalayer.default_NetLibDB_Projeler_ConnectionString, stSQL, ref rowsAffected))
                {
                    MessageBox.Show(this, "Source type added successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cmbType.Items.Clear();
                    FillComboBox(ref cmbType);
                }
                
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim() != "" && cmbType.SelectedIndex >= 0)
            {
                VarTypes vt = (VarTypes)cmbType.SelectedItem;
                stSQL = "INSERT INTO visual_auditory_resources (var_name, var_type) VALUES ( '" + txtName.Text.Trim() + "' , " + vt.ID + " )";

                if (datalayer.Execute(datalayer.default_NetLibDB_Projeler_ConnectionString, stSQL, ref rowsAffected))
                {
                    MessageBox.Show(this, "V&A Source  added successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    lvwSources.Items.Clear();
                    FillListView(ref lvwSources);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lvwSources.SelectedIndices.Count > 0)
            {
                string var_id = lvwSources.SelectedItems[0].Tag.ToString().Trim();

                stSQL = "DELETE FROM visual_auditory_resources WHERE var_id = " + var_id;

                DialogResult dr = MessageBox.Show(this, "Do you really want to delete ?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (dr.ToString() == "OK")
                {
                    if (datalayer.Execute(datalayer.default_NetLibDB_Projeler_ConnectionString, stSQL, ref rowsAffected))
                    {
                        MessageBox.Show(this, "V&A Source  deleted successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        lvwSources.Items.Clear();
                        FillListView(ref lvwSources);
                    }
                }
            }
        }
    }

    class VarTypes
    {
        string id;
        string description;

        public string ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }
        public override string ToString()
        {
            return description;
        }
    }
}